﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zhuang.Model.EF;
using Zhuang.DAL;
using Zhuang.Utility.Common.Encryption;

namespace Zhuang.WebSite.BLL.SystemManagement
{
    public class Sys_UserBLL
    {

        public Sys_UserDAL sys_UserDAL=new Sys_UserDAL();


        public DataTable GetPageOfUser(int page, int pageSize, ref int totalRowCount, string conditions)
        {
            return sys_UserDAL.GetPage(page,pageSize,ref totalRowCount,conditions);
        }

        public Sys_User GetEntity(int id)
        {
            Sys_User user = null;

            DataTable dt = sys_UserDAL.Get(id);

            if (dt.Rows.Count > 0)
            {
                user = new Sys_User();
                user.UserId = Convert.ToInt32(dt.Rows[0]["userid"]);
                user.UserName = dt.Rows[0]["userName"].ToString();
                user.Password = dt.Rows[0]["password"].ToString();
                user.Name = dt.Rows[0]["name"].ToString();
                user.Gender = dt.Rows[0]["gender"].ToString();
                user.Address = dt.Rows[0]["address"].ToString();
                user.Birthday = dt.Rows[0]["birthday"].ToString();
            }

            return user;
        }

        public int UpdateEntity(Sys_User user)
        {
            user.Password = AES.Encode(user.Password, null);
            return sys_UserDAL.Update(user.UserId,user.Password,user.Name,user.Gender,user.Address,user.Birthday);
        }

        public int DeleteEntity(int id)
        {
            return sys_UserDAL.Delete(id);
        }

        public int DeleteEntity(Sys_User user)
        {
            return sys_UserDAL.Delete(user.UserId);
        }

        public int AddEntity(Sys_User user)
        {
            if (sys_UserDAL.ExistsUserName(user.UserName))
            {
                user.Password = AES.Encode(user.Password,null);
                return sys_UserDAL.Add(user.UserName,user.Password,user.Name,user.Gender,user.Address,user.Birthday);
            }
            else
            {
                return 0;
            }
        }

        public DataTable GetLoginUser(string moduleType,string username, string password)
        {
            password = AES.Encode(password,null);
            return sys_UserDAL.GetLoginUser(moduleType,username, password);
        }


        public List<Sys_Menu> GetUserMenus(string UserId)
        {
            DataTable dt = sys_UserDAL.GetUserMenus(UserId);

            List<Sys_Menu> userMenus = Zhuang.Utility.Common.Entity.EntityHelper<Sys_Menu>.FillEntity(dt);
            return userMenus;
        }


        public DataTable GetUserRoles(int UserId)
        {

            return sys_UserDAL.GetUserRoles(UserId);
        }

        public void SaveUserRoles(int UserId,DataTable dtUserRoles)
        {
            sys_UserDAL.SaveUserRoles(UserId,dtUserRoles);
        }

    }
}
